package com.controller;

import com.model.qrcode.ActionInfo;
import com.model.qrcode.Qrode;
import com.model.qrcode.Scene;
import com.service.qrode.QrodeService;
import com.thread.AccessTokenThread;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/qrode")
public class QrodeController {
    @Autowired
    private QrodeService qrodeService;

    private static Logger log = LoggerFactory.getLogger(QrodeController.class);

    //测试
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        if(at!=null){
            JSONObject jsonObject=qrodeService.add(getTemporaryQrode(),at);

            return jsonObject.toString();
        }else {
            log.error("创建二维码失败失败");
            return "创建二维码失败";
        }
    }

    //创建临时的二维码，获取其ticket值。有效时间为3天
    @RequestMapping(value = "/addtem",method = RequestMethod.POST)
    public String addTemporary(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        if(at!=null){
            JSONObject jsonObject=qrodeService.add(getTemporaryQrode(),at);

            return jsonObject.getString("ticket");
        }else {
            log.error("创建二维码失败失败");
            return "创建二维码失败";
        }
    }
    //获取临时的二维码，有效时间为3天
    //使用重定向跳转到微信获取二维码的地址
    //直接弹到二维码图片上
    @RequestMapping(value = "/gettem",method = RequestMethod.GET)
    public ModelAndView getTemporary() {
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        String ticket = null;
        if (at != null) {
            JSONObject jsonObject = qrodeService.add(getTemporaryQrode(), at);
            ticket = jsonObject.getString("ticket");
            String url="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
            ModelAndView mv=new ModelAndView("redirect:"+url);
            log.info("获取临时二维码成功，他的ticket为："+jsonObject.getString("ticket"));


            return mv;
        } else {
            log.error("获取临时二维码失败");
            return null;
        }
    }

    //创建永久的二维码
    @RequestMapping(value = "/addper",method = RequestMethod.POST)
    public String addPermanent(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();

        if(at!=null){
            JSONObject jsonObject=qrodeService.add(getPermanentQrode(),at);

            return jsonObject.getString("ticket");
        }else {
            log.error("创建二维码失败失败");
            return "创建二维码失败";
        }
    }
    //获取上面的永久的二维码
    @RequestMapping(value = "/getper",method = RequestMethod.GET)
    public ModelAndView getPermanent(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        String ticket = null;
        if (at != null) {
            JSONObject jsonObject = qrodeService.add(getPermanentQrode(), at);
            ticket = jsonObject.getString("ticket");
            String url="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
            ModelAndView mv=new ModelAndView("redirect:"+url);
            log.info("获取临时二维码成功，他的ticket为："+jsonObject.getString("ticket"));
            return mv;
        } else {
            log.error("获取临时二维码失败");
            return null;
        }
    }


    //创建有效期为3天的二维码,
    public  static Qrode getTemporaryQrode(){
        Qrode qrode=new Qrode();
        qrode.setAction_name("QR_SCENE");//QR_SCENE为临时,QR_LIMIT_SCENE为永久,QR_LIMIT_STR_SCENE为永久的字符串参数值
        qrode.setExpire_seconds("2592000000");//此二维码三天后失效
        ActionInfo actionInfo=new ActionInfo();
        Scene scene=new Scene();
        scene.setScene_str("123");
        actionInfo.setScene(new ActionInfo[]{scene});
        qrode.setActioninof(new Qrode[]{actionInfo});

        return qrode;

    }//创建永久的二维码
    public Qrode getPermanentQrode(){
        Qrode qrode=new Qrode();
        qrode.setAction_name("QR_LIMIT_SCENE");//QR_SCENE为临时,QR_LIMIT_SCENE为永久,QR_LIMIT_STR_SCENE为永久的字符串参数值
        ActionInfo actionInfo=new ActionInfo();
        Scene scene=new Scene();
        scene.setScene_str("123");
        actionInfo.setScene(new ActionInfo[]{scene});
        qrode.setActioninof(new Qrode[]{actionInfo});
        return qrode;

    }

}
